﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using FILSTARTUPS_MOBILE.Models;

namespace FILSTARTUPS_MOBILE.ViewModels
{
    /**** CLASS FOR CALLING METHODS IN REST API CREDIT OTTER APP *****/
    public class FilStartUps
    {
        //DEFAULT URL
        private const string BaseUrl = "http://192.168.0.109:8099/FilstartUpsRest/filstartupsservice/rest/";

        private static string GetJson(string url)
        {
            WebClient wc= new WebClient();
            string json = wc.DownloadString(url);
            wc.Dispose();
            return json;
        }

        //GET PAGESUB/CATEGORY LATEST ARTICLE
        public static CategoryPost GetCategoryPost(int id)
        {
            string fullUrl = BaseUrl + "category/GetCategoryPost/" + id;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var subpage_post = JsonConvert.DeserializeObject<CategoryPost>(stuff["catpost"].ToString());
            return subpage_post;
        }

        //Category Page
        //return List = catidPK, categoryname
        public static List<CategoryInfo> GetPageSub(int id)
        {
            string fullUrl = BaseUrl + "category/GetPageSub/" + id;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var subpage = JsonConvert.DeserializeObject<List<CategoryInfo>>(stuff["pagesub"].ToString());
            return subpage;
        }

        //ForumMainTopic
        //return list = forumidPK, forumname, forumdescription
        public static List<Forum> GetForumTopic()
        {
            string fullUrl = BaseUrl + "forum/GetForumTopic " ;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var forum = JsonConvert.DeserializeObject<List<Forum>>(stuff["forum"].ToString());
            return forum;
        }
        //PostArchieves
        //return List = postidPK, posttitle, postdescription
        public static List<PostInfo> GetArticleArchieves(int catidFK)
        {
            string fullUrl = BaseUrl + "post/GetArticleArchieves/" + catidFK;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var article = JsonConvert.DeserializeObject<List<PostInfo>>(stuff["archieves"].ToString());
            return article;
        }
        
        //Get Article Details
        //return CategoryPost Class
        public static CategoryPost GetArticleDetails(int postidPK)
        {
            string fullUrl = BaseUrl + "post/GetPostDetails/" + postidPK;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var article = JsonConvert.DeserializeObject<CategoryPost>(stuff["data"].ToString());
            return article;
        }

        //List of Nav
        //return pageidPK, pagename
        public static List<PageNavBar> GetPageNav()
        {
            string fullUrl = BaseUrl + "page/getpageinfo";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var page = JsonConvert.DeserializeObject<List<PageNavBar>>(stuff["data"].ToString());
            return page;
        }
        //Removed Pagename
        //GetPageInformation 
        //return pagedescription,pagename, pageidPK
        public static PageInfo GetPageInfo(int pageidPK)
        {
            string fullUrl = BaseUrl + "page/GetPageFullInfo/" + pageidPK;
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var article = JsonConvert.DeserializeObject<PageInfo>(stuff["pageinfo"].ToString());
            return article;
        }

        //Get Latest News
        //return postidPK, posttitle, postdescription
        public static List<PostInfo> GetNewPost()
        {
            string fullUrl = BaseUrl + "post/getnewsdetails";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var news = JsonConvert.DeserializeObject<List<PostInfo>>(stuff["data"].ToString());
            return news;
        }

        //Get Latest Articles
        //return postidPK, posttitle, postdescription
        public static List<PostInfo> GetLatestArticles()
        {
            string fullUrl = BaseUrl + "post/getlatestarticles";
            string json = GetJson(fullUrl);
            JObject stuff = JObject.Parse(json);
            var news = JsonConvert.DeserializeObject<List<PostInfo>>(stuff["data"].ToString());
            return news;
        }

        #region Refactor For Retrieving Of Data
        /*** UNDERCONSTRUCTION LATEST POST WAITING FOR JSON.NET ---> GETPOSTFULLINFO();
        public static PostInfo GetPostInformation()
        {
            try
            {
                string fullUrl = BaseUrl + "post/getpostdetails";
                string json = GetJson(fullUrl);
                JObject stuff = JObject.Parse(json);
                var resp = JsonConvert.DeserializeObject<PostInfo>(stuff["data"].ToString());
                return resp;
            }
            catch
            {
                throw;
            }
        }
         *****************/

        /*** UNDERCONSTRUCTION LATEST NEWS --> GETNEWSFULLINFO();
        public static PostInfo GetNewsInformation()
        {
            try
            {
                string fullUrl = BaseUrl + "post/getnewsdetails";
                string json = GetJson(fullUrl);
                JObject stuff = JObject.Parse(json);
                var resp = JsonConvert.DeserializeObject<PostInfo>(stuff["data"].ToString());
                return resp;
            }
            catch
            {
                throw;
            }
        }
         ******************/

        /*** UNDERCONSTRUCTION LATEST EVENTS --> GETEVENTSFULLINFO(); 
        public static PostInfo GetEventsInformation()
        {
            try
            {
                string fullUrl = BaseUrl + "post/geteventsdetails";
                string json = GetJson(fullUrl);
                JObject stuff = JObject.Parse(json);
                var resp = JsonConvert.DeserializeObject<PostInfo>(stuff["data"].ToString());
                return resp;
            }
            catch
            {
                throw;
            }
        }
        *****/
        /***************** PAGEINFO 
        public static PageInfo GetPageInfo()
        {
            try
            {
                string fullUrl = BaseUrl + "page/getpageinfo";
                string json = GetJson(fullUrl);
                JObject stuff = JObject.Parse(json);
                var resp = JsonConvert.DeserializeObject<PageInfo>(stuff["pageidPK"]["pagedescription"]["pagename"].ToString());
                return resp;
            }
            catch
            {
          
            }
        }
        ******************/

        #endregion
    }
}